﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using offline2048;
using UnityEngine;

namespace GTA
{
    public class ActionHelper : Singleton<ActionHelper>
    {

        public static void SendFireBaseProperty() {

        }

        public static void SendAction(string actionName, Dictionary<string, object> dict)
        {
            /*if (dict == null)
            {
                dict = new Dictionary<string, object>();
            }
            dict["actionTime"] = Utils.GetTimeStamp();
            LogEvent(actionName, dict);*/
        }

        public static void LogEvent(string actionName, Dictionary<string, object> action)
        {
            string eventName = actionName;

            //Debug.Log($"shengming ActionHelper id = {PublicDataMgr.Instance.eventNumber} , eventName = {eventName}");

            Dictionary<string, object> dictPublic = new Dictionary<string, object>();//PublicDataMgr.Instance.GetPublicData();
            
            Dictionary<string, object> dictPrivate = new Dictionary<string, object>();

            //Debug.Log("shengming ActionHelper id =" + dictPublic["en"]);

            if (eventName.Equals("install"))
            {
/*                int installTimes = GameDataMgr.Instance.installTimes;
                dictPrivate.Add("v1", installTimes);
                AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                dictPrivate.Add()*/

                AnalyticsHelper.LogEvent("SENSORSBI", eventName,null);
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
                AnalyticsHelper.LogEvent("FIREBASE", "custom_install", null);
                //AnalyticsHelper.LogEvent("APPSFLYER", "af_install", null);
                AnalyticsHelper.LogEvent("APPSFLYER", "u6j4v3", null);
            }
            else if (eventName.Equals("open"))
            {

            }
            else if (eventName.Equals("close"))
            {

            }
            else if (eventName.Equals("ad_view_b"))
            {
/*                dictPrivate.Add("v1", string.IsNullOrEmpty(AdWrapper.Instance.BannerNetWorkName) ? "empty" : AdWrapper.Instance.BannerNetWorkName);
                dictPrivate.Add("v2", AdsDataMgr.Instance.AdBannerViewTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdBannerViewCount);*/

                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdBannerViewTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdBannerViewCount);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
                //AnalyticsHelper.LogEvent("APPSFLYER", "af_ad_view_b", null);
                AnalyticsHelper.LogEvent("APPSFLYER", "ukzuj9", null);
                Dictionary<string, object> dict = new Dictionary<string, object>() {
                    {"aj_adrev_ad_type", "banner"},
                };
                AnalyticsHelper.LogEvent("APPSFLYER", "aj_ad_view", dict);
            }
            else if (eventName.Equals("ad_click_b"))
            {
                Dictionary<string, object> dict = new Dictionary<string, object>() {
                };
                AnalyticsHelper.LogEvent("APPSFLYER", "5ltsbk", dict);
            }
            else if (eventName.Equals("ad_view_i"))
            {
                dictPrivate.Add("from",action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialViewTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialViewCount);
                
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                var firebaseParams = new Dictionary<string, object>(action);
                AnalyticsHelper.LogEvent("FIREBASE", eventName, firebaseParams);

                Dictionary<string, object> dict = new Dictionary<string, object>() {

                };
                //AnalyticsHelper.LogEvent("APPSFLYER", "af_ad_view_i", dict);
                AnalyticsHelper.LogEvent("APPSFLYER", "4jtmw2", dict);
                Dictionary<string, object> dict1 = new Dictionary<string, object>() {
                    {"aj_adrev_ad_type", "inter"},
                };
                AnalyticsHelper.LogEvent("APPSFLYER", "aj_ad_view", dict1);
                //AnalyticsHelper.LogEvent("APPSFLYER", "k70r4k", null);
            }
            else if (eventName.Equals("ad_click_i"))
            {
                dictPrivate.Add("from",action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialClickTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialClickCount);
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
                Dictionary<string, object> dict = new Dictionary<string, object>() {

                };
                //AnalyticsHelper.LogEvent("APPSFLYER", "af_ad_click_i", dict);
                AnalyticsHelper.LogEvent("APPSFLYER", "xyfipc", dict);
            }
            else if (eventName.Equals("ad_close_i"))
            {
                dictPrivate.Add("from",action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialCloseTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialCloseCount);
                dictPrivate.Add("Playtime", action["watchTime"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_viewback_i"))
            {
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialViewBackTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialViewBackCount);
                dictPrivate.Add("Playtime", action["watchTime"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_usetime_i"))
            {
                dictPrivate.Add("Clickthrough", action["clickthrough"]);
                dictPrivate.Add("Playtime", action["playTime"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_view_rv"))
            {
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoViewTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoViewCount);

                var firebaseParams = new Dictionary<string, object>(action);
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, firebaseParams);

                Dictionary<string, object> dict = new Dictionary<string, object>() {

                };
                AnalyticsHelper.LogEvent("APPSFLYER", "ofruzd", dict);
                Dictionary<string, object> dict1 = new Dictionary<string, object>() {
                    {"aj_adrev_ad_type", "reward"},
                };
                AnalyticsHelper.LogEvent("APPSFLYER", "aj_ad_view", dict1);
                
            }
            else if (eventName.Equals("ad_click_rv"))
            {
/*                dictPrivate.Add("v1", AdWrapper.Instance.GetRewardWorkName());
                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoClickTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoClickCount);
                dictPrivate.Add("v4", action["from"]);*/
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoClickTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoClickCount);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);

                Dictionary<string, object> dict = new Dictionary<string, object>() {

                };
                //AnalyticsHelper.LogEvent("APPSFLYER", "af_ad_click_rv", dict);
                AnalyticsHelper.LogEvent("APPSFLYER", "kraiwf", dict);
            }
            else if (eventName.Equals("ad_close_rv"))
            {
/*                dictPrivate.Add("v1", AdWrapper.Instance.GetRewardWorkName());
                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoCloseTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoCloseCount);
                dictPrivate.Add("v4", action["from"]);
                dictPrivate.Add("v5", int.Parse((string)action["finish"]));
                dictPrivate.Add("v6", action["watchTime"]);*/
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoCloseTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoCloseCount);
                dictPrivate.Add("Finish", int.Parse((string)action["finish"]));
                dictPrivate.Add("Playtime", action["watchTime"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_viewback_rv"))
            {
/*                dictPrivate.Add("v1", action["network"]);
                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoViewBackTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoViewBackCount);
                dictPrivate.Add("v4", action["from"]);
                dictPrivate.Add("v5", action["finish"]);
                dictPrivate.Add("v6", action["watchTime"]);*/
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoViewBackTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoViewBackCount);
                dictPrivate.Add("Finish", action["finish"]);
                dictPrivate.Add("Playtime", action["watchTime"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_will_view_i"))
            {
                //dictPrivate.Add("v1", MopubWrapper.Instance.netWorkName);
/*                dictPrivate.Add("v2", AdsDataMgr.Instance.AdInterstitialWillViewTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdInterstitialWillViewCount);
                dictPrivate.Add("v4", action["from"]);
                dictPrivate.Add("v5", ADSHelper.IsInterstitialAdLoaded() ? 1 : 0);*/
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialWillViewTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialWillViewCount);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_will_view_rv"))
            {
                //dictPrivate.Add("v1", MopubWrapper.Instance.netWorkName);
/*                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoWillViewTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoWillViewCount);
                dictPrivate.Add("v4", action["from"]);
                dictPrivate.Add("v5", action["rvload"]);*/
                dictPrivate.Add("from", action["from"]);
                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoWillViewTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoWillViewCount);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_failed_b"))
            {
                //dictPrivate.Add("v1", MopubWrapper.Instance.BannerNetWorkName);
/*                dictPrivate.Add("v2", AdsDataMgr.Instance.AdBannerFailedTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdBannerFailedCount);
                dictPrivate.Add("v4", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("v5", action["FailedInterval"]);*/

                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdBannerFailedTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdBannerFailedCount);
                dictPrivate.Add("HoldConnect", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("FailedInterval", action["FailedInterval"]);


                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_failed_i"))
            {
                //dictPrivate.Add("v1", MopubWrapper.Instance.GetInterstitialWorkName());
/*                dictPrivate.Add("v2", AdsDataMgr.Instance.AdInterstitialFailedTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdInterstitialFailedCount);
                dictPrivate.Add("v4", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("v5", action["FailedInterval"]);*/

                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdInterstitialFailedTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdInterstitialFailedCount);
                dictPrivate.Add("HoldConnect", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("FailedInterval", action["FailedInterval"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
            else if (eventName.Equals("ad_failed_rv"))
            {
                //dictPrivate.Add("v1", MopubWrapper.Instance.GetRewardWorkName());
/*                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoFailedTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoFailedCount);
                dictPrivate.Add("v4", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("v5", action["FailedInterval"]);*/

                dictPrivate.Add("TodayNum", AdsDataMgr.Instance.AdRewardVideoFailedTodayCount);
                dictPrivate.Add("HistoryNum", AdsDataMgr.Instance.AdRewardVideoFailedCount);
                dictPrivate.Add("HoldConnect", Application.internetReachability == NetworkReachability.NotReachable ? "offline" : "online");
                dictPrivate.Add("FailedInterval", action["FailedInterval"]);

                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }
/*            else if (eventName.Equals("ad_playfailed_rv"))
            {
                dictPrivate.Add("v1", AdWrapper.Instance.GetRewardWorkName());
                dictPrivate.Add("v2", AdsDataMgr.Instance.AdRewardVideoPlayFailedTodayCount);
                dictPrivate.Add("v3", AdsDataMgr.Instance.AdRewardVideoPlayFailedCount);
                AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
            }*/
            else if (eventName.Equals("iap_begin"))
            {
                dictPrivate = new Dictionary<string, object>(action);
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);

                double price = Convert.ToDouble(action["Price"]) * 0.7;
                var dict = new Dictionary<string, object>(){
                    {"aj_price", price.ToString("F", CultureInfo.InvariantCulture)}
                };
                AnalyticsHelper.LogEvent("APPSFLYER", "aj_initiated_checkout", dict);
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
            }
            else if (eventName.Equals("iap_end"))
            {
                dictPrivate = new Dictionary<string, object>(action);
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);

                //成功内购后发送,标记测试设备后不发送
                if ((int)action["Result"] == 1 && !GameDataMgr.Instance.IsDebugDevice)
                {
                    double price = Convert.ToDouble(action["Price"]) * 0.7;
                    var dict = new Dictionary<string, string>(){
                        {"aj_revenue", price.ToString("F", CultureInfo.InvariantCulture)}
                    };
                    //AnalyticsHelper.LogEvent("APPSFLYER", "af_purchase", dict);
                    AnalyticsHelper.AdjustLogEventRevenue("kvc5j2", dict, price, "USD");
                }
            }
            else if (eventName.Equals("send_message"))
            {
                dictPrivate = new Dictionary<string, object>(action);
                //AnalyticsHelper.LogEvent("COUNTLY", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName, null);
                AnalyticsHelper.LogEvent("SENSORSBI", eventName, Utils.MergeDictionary(dictPublic, dictPrivate));
            }
            else
            {
                GameLogger.Error("[" + eventName + "] 未找到匹配的事件，请检查事件名！");

                //AnalyticsHelper.LogEvent("COUNTLY", eventName + "_nameWrong", Utils.MergeDictionary(dictPublic, dictPrivate));
                AnalyticsHelper.LogEvent("FIREBASE", eventName + "_nameWrong", null);
                AnalyticsHelper.LogEvent("SENSORSBI", eventName + "_nameWrong", Utils.MergeDictionary(dictPublic, dictPrivate));
            }

            LogGTAEvent(eventName, dictPrivate, dictPublic);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dic"></param>
        public static void LogGTAEvent(string name, Dictionary<string, object> dictPrivate, Dictionary<string, object> dictPublic)
        {
            string t = "";
            foreach (var item in dictPrivate)
            {
                t += $"\r\n\t{item.Key}:{item.Value}";
            }
            t = $"[{name}]   [{DateTime.Now.ToString()}] {t}";

            GameLogger.Log(MotionFramework.Console.GameLogType.GTA, t);
        }
    }
}
